package com.collaboration.proton.controllers;

import com.collaboration.proton.dao.GameZoneRepository;
import com.collaboration.proton.dao.HeroRepository;
import com.collaboration.proton.dao.PlayerInformationRepository;
import com.collaboration.proton.entities.*;
import com.collaboration.proton.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author vybfi
 * @param
 * @return 无
 * 这个类是主页的控制层,用于接受主页的数据输入
 */
@Controller
@RequestMapping("home")
public class HomeController {

    @Autowired
    PlayerService playerService;

    @Autowired
    SingleAgencyInfoService singleAgencyInfoService;

    @Autowired
    HeroService heroService;

    @Autowired
    GameZoneService gameZoneService;

    @Autowired
    PlayerInformationService playerInformationService;

    @Autowired
    UserHeroService userHeroService;

    @GetMapping()
    public String displayHome(Model model, Principal principal){

//        String name=SecurityContextHolder.getContext().getAuthentication().getName();
        String name = principal.getName();
        model.addAttribute("name",name);

        Player player = playerService.findPlayerByUserName(name);
        long Id = player.getUserId();
        model.addAttribute("Id",Id);

//        PlayerInformation = playerInformationRepository.findAll().get(1);

//        if (playerInformationService.findPlayerInformationByPlayer(player) == null) {
//            PlayerInformation playerInformation =new PlayerInformation();
//            playerInformation.setGrade(0);
//            playerInformation.setWinRate(0);
//            model.addAttribute("playerInformation",playerInformation);
//        }else {}
            PlayerInformation playerInformation =
                    playerInformationService.findPlayerInformationByPlayer(player);
            model.addAttribute("playerInformation",playerInformation);



        List<SingleAgencyInfo> singleAgencyInfos =
                singleAgencyInfoService.findAllByPlayer(player);
        model.addAttribute("singleAgencyInfos",singleAgencyInfos);


//        if (userHeroService.findUserHeroByPlayer(player) == null) {
//            List<Hero> heroes=new ArrayList<>();
//            model.addAttribute("heroes",heroes);
//        }else{}
            List<Hero> heroes = heroService.findAllByUserHero(
                    userHeroService.findUserHeroByPlayer(player));
            model.addAttribute("heroes",heroes);




        List<GameZone> gameZones = gameZoneService.getAll();
        model.addAttribute("gameZones",gameZones);



        return "main/home";
    }

//    SecurityContextHolder.getContext().getAuthentication().getName();

}
